﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace 包覆门
{
    public partial class addorder : Form
    {
        public addorder()
        {
            InitializeComponent();
        }
        public static String connetStr = "server=192.168.11.91;port=3306;user=bfm;password=123456; database=baofumen;";
        MySqlConnection ms = new MySqlConnection(connetStr);
        private void addorder_Load(object sender, EventArgs e)
        {
            try
            {
                ms.Open();
                String str = "select * from bprice where type=0";
                MySqlDataAdapter ad = new MySqlDataAdapter(str, ms);
                DataSet dsdx = new DataSet();
                ad.Fill(dsdx);
                String str1 = "select * from bprice where type=1";
                MySqlDataAdapter adfj = new MySqlDataAdapter(str1, ms);
                DataSet dsfj = new DataSet();
                adfj.Fill(dsfj);
                //this.dataGridView1.DataSource = ds.Tables[0];
                this.menxing.DataSource = dsdx.Tables[0];
                this.menxing.DisplayMember = "mxname";
                this.menxing.ValueMember = "id";

                this.fujian.DataSource = dsfj.Tables[0];
                this.fujian.DisplayMember = "mxname";
                this.fujian.ValueMember = "id";
            }
            catch (MySqlException me)
            {
                MessageBox.Show(me.Message);
            }
            finally {
                ms.Close();
            }
            

        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {
            
            DataTable dt = GetDgvToDatable(dgorder);

            try
            {
                ms.Open();

            }
            catch (Exception)
            {
                
                throw;
            }


        }

        private void btnadd_Click(object sender, EventArgs e)
        {
            dgorder.Rows.Add();
        }

        private void btnremove_Click(object sender, EventArgs e)
        {
            foreach( DataGridViewRow dr in dgorder.SelectedRows ){
                if (dr.IsNewRow == false)//如果不是已提交的行，默认情况下在添加一行数据成功后，DataGridView为新建一行作为新数据的插入位置
                    dgorder.Rows.Remove(dr);
            }
        }

        public static DataTable GetDgvToDatable(DataGridView dgv) { 
            DataTable dt = new DataTable();
            for(int count =0 ;count<dgv.ColumnCount;count++){
                DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                dt.Columns.Add(dc);
            }
            for(int i =0;i<dgv.Rows.Count;i++){
                DataRow dr = dt.NewRow();
                for(int j=0;j<dgv.Columns.Count;j++){
                    dr[j]=dgv.Rows[i].Cells[j].Value;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        private void addorder_FormClosing(object sender, FormClosingEventArgs e)
        {
            //mainform.adr = null;
        }

        
    }
}
